package com.tjtt.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;

import javax.servlet.http.HttpServletResponse;

import com.tjtt.tdm.base.Constants;

/**
 * Http 相关操作
 */
public class HttpUtil {

	/**
	 * HTTP下载XML
	 * @param response
	 * @param file 下载的文件，只适合String类型
	 * @param fileName 文件名
	 * @param encoding 文件和文件名的编码
	 * @throws IOException void
	 * @author 王明龙
	 */
	public static void downloadXML(HttpServletResponse response, String file, String fileName, String encoding)
			throws IOException {
		String urlFileName = URLEncoder.encode(fileName, Constants.ENCODING);
		if (urlFileName.length() > 150) {
			String guessCharset = "GBK"; /*根据request的locale 得出可能的编码，中文操作系统通常是gb2312*/
			urlFileName = new String(fileName.getBytes(guessCharset), "ISO8859-1");
		}
		response.setContentType("application/x-download;charset=" + Constants.ENCODING);
		response.setCharacterEncoding(Constants.ENCODING);
		response.addHeader("Content-Disposition", "attachment;filename=" + urlFileName);
		response.getWriter().write(file);
	}

	/**
	 * 下载文件
	 * @param response
	 * @param path 路径
	 * @param fileName 文件名
	 * @param encoding 文件编码
	 * @throws IOException void
	 * @author 王明龙
	 */
	public static void download(HttpServletResponse response, String path, String fileName, String encoding)
			throws IOException {
		String urlFileName = URLEncoder.encode(fileName, Constants.ENCODING);
		if (urlFileName.length() > 150) {
			String guessCharset = "GBK"; /*根据request的locale 得出可能的编码，中文操作系统通常是gb2312*/
			urlFileName = new String(fileName.getBytes(guessCharset), "ISO8859-1");
		}
		response.reset();// 可以加也可以不加
		OutputStream output = null;
		FileInputStream fis = null;
		try {
			fis = new FileInputStream(path);
			response.setContentType("application/x-download");// 设置为下载application/x-download
			response.addHeader("Content-Disposition", "attachment;filename=" + urlFileName);
			output = response.getOutputStream();
			byte[] b = new byte[1024];
			int i = 0;
			while ((i = fis.read(b)) > 0) {
				output.write(b, 0, i);
			}
			output.flush();
		}
		catch (RuntimeException e) {
			throw e;
		}
		finally {
			if (fis != null) {
				fis.close();
				fis = null;
			}
			if (output != null) {
				output.close();
				output = null;
			}
		}
	}

	public static void downImg(HttpServletResponse response, String path) throws IOException {
		
		OutputStream output = null;
		FileInputStream fis = null;
		byte[] b = new byte[8*1024];
		try {
			output = response.getOutputStream();
			
			fis = new FileInputStream(path);
			response.reset();// 可以加也可以不加
			int len = 0; 
			while((len = fis.read(b))!=-1)
			{ 
				output.write(b,0,len);
			} 
			//BufferedImage img = ImageIO.read(fis);
			//ImageIO.write(img, "image/pjpeg", output);
		}
		catch (IOException e) {
			throw e;
		}
		finally {
			if (fis != null) {
				fis.close();
				fis = null;
			}
			if (output != null) {
				output.close();
				output = null;
			}
		}
	}




}
